package com.centling.smartSealForPhone.db;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.centling.smartSealForPhone.constants.SPConstants;
import com.centling.smartSealForPhone.utils.LogUtil;
import com.centling.smartSealForPhone.utils.SPUtil;

import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.util.Locale;

public class ImageBean
        implements Serializable {
    private int tId;
    private String type;
    private String taskId;
    private String userId;
    private String backId;
    private String companyCode;
    private String paths;
    private String date;
    private Bitmap mBitmap;

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getCompanyCode() {
        return companyCode;
    }

    public void setCompanyCode(String companyCode) {
        this.companyCode = companyCode;
    }

    public String getBackId() {
        return backId;
    }

    public void setBackId(String backId) {
        this.backId = backId;
    }

    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public int getId() {
        return tId;
    }

    public void setId(int tId) {
        this.tId = tId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPaths() {
        return paths;
    }

    public void setPaths(String paths) {
        this.paths = paths;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return "file";
    }

    public String getFileName() {
        String fileType = paths.substring(paths.lastIndexOf(".") + 1);
        return type + "_" + taskId + "_" + userId + "_" + date + "_" + backId + "_" + companyCode + "." + fileType;
    }

    /**
     * 图片压缩
     */
    public byte[] getValue() {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            boolean isHighQuality = SPUtil.getBoolean(SPConstants.HQ_UPLOAD, true);
            mBitmap = decodeSampledBitmapFromPath(paths, isHighQuality);
            mBitmap.compress(Bitmap.CompressFormat.JPEG, isHighQuality ? 75 : 60, bos);
            LogUtil.d("压缩后的图片大小：" + bos.toByteArray().length / 1024 + "KB");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bos.toByteArray();
    }

    private Bitmap decodeSampledBitmapFromPath(String path, boolean isHighQuality) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        int a = isHighQuality ? 720 : 540;
        int b = isHighQuality ? 1280 : 960;
        if (options.outWidth > options.outHeight) {
            a = isHighQuality ? 1280 : 960;
            b = isHighQuality ? 720 : 540;
        }
        options.inSampleSize = calculateInSampleSize(options, a, b);
        LogUtil.d(String.format(Locale.CHINA, "压缩前的图片大小：%dKB %d %d %d",
                options.outWidth * options.outHeight * 2 / 1024, options.inSampleSize, a, b));
        options.inJustDecodeBounds = false;
        options.inPreferredConfig = Bitmap.Config.RGB_565;

        return BitmapFactory.decodeFile(path, options);
    }

    private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        int width = options.outWidth;
        int height = options.outHeight;
        int inSampleSize = 1;
        if (width > reqWidth || height > reqHeight) {
            int widthRadio = Math.round(width * 1.0f / reqWidth);
            int heightRadio = Math.round(height * 1.0f / reqHeight);

            inSampleSize = Math.max(widthRadio, heightRadio);
        }
        return inSampleSize;
    }

    public void recycle() {
        if (mBitmap != null) {
            mBitmap.recycle();
            mBitmap = null;
        }
    }
}
